#!/usr/bin/perl
# -*-perl-*-

use strict;
use lib 'lib';
use Net::PubSubHubbub::Publisher;
use Getopt::Long;
use LWP::Simple ();

sub usage {
    my $err = shift;
    if ($err) {
        warn "ERROR: $err\n\n";
    }
    print STDERR <<END;
Usage: pubsubhubbub-publish [OPTS] <topic_url> [<topic_url2> ...]

Pings the hub, notifying the hub that the provided 'topic_url' has
been updated.

Options:
--hub=<hub_publish_endpoint>  Which hub endpoint to ping.  Defaults
    to the open, reference hub, but you need to use whatever hub
    that your Topic URL references.

END

    exit(1);
}

my $hub = "http://pubsubhubbub.appspot.com/";
GetOptions("hub=s" => \$hub)
    or usage();

my @topic_urls = @ARGV or usage("topic_url required.");
foreach my $url (@topic_urls) {
    usage("Bogus topic URL: $url")
        unless $url =~ m!^https?://\S+$!;
}

usage("No hub provided.")
    unless $hub && $hub =~ m!^https?://\S+$!;

my $publisher = Net::PubSubHubbub::Publisher->new(hub => $hub);
unless ($publisher->publish_update(@topic_urls)) {
    warn "Error pinging hub: " . $publisher->last_response->status_line;
    exit(1);
}
